package com.zja.dao.Impl;

import com.zja.dao.BaseDAO;
import com.zja.dao.FavoriteDao;
import com.zja.model.Favorite;

import java.util.List;

public class FavoriteDaoImpl extends BaseDAO implements FavoriteDao {
    @Override
    public List<Favorite> selectAll() {
        String sql = "SELECT * FROM favorite";
        try {
            return executeQuery(Favorite.class, sql);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Favorite selectById(Long id) {
        try {
            String sql = "SELECT * FROM favorite WHERE id = ?";
            return executeQueryBean(Favorite.class, sql, id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean insert(Favorite favorite) {
        try {
            String sql = "INSERT INTO favorite (following_id, followed_id, knowledge_base_id, article_id, created_at) VALUES (?, ?, ?, ?, ?)";
            return executeUpdate(sql, favorite.getFollowingId(), favorite.getFollowedId(), favorite.getKnowledgeBaseId(), favorite.getArticleId(), favorite.getCreatedAt());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean update(Favorite favorite) {
        try {
            String sql = "UPDATE favorite SET following_id = ?, followed_id = ?, knowledge_base_id = ?, article_id = ?, created_at = ? WHERE id = ?";
            return executeUpdate(sql, favorite.getFollowingId(), favorite.getFollowedId(), favorite.getKnowledgeBaseId(), favorite.getArticleId(), favorite.getCreatedAt(), favorite.getId());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean deleteById(Long id) {
        try {
            String sql = "DELETE FROM favorite WHERE id = ?";
            return executeUpdate(sql, id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
